// 使用 Mock
var Mock = require("mockjs");
const Koa = require("koa");
const Router = require("@koa/router");
var bodyParser = require("koa-bodyparser");
const jwt = require('jsonwebtoken');
const app = new Koa();
const router = new Router();

//token
const tokenuser = (userinfo) => {
	const token = jwt.sign(
		{
			username: userinfo.email,
			password: userinfo.username
		},
		'serect',
        { expiresIn: '1h' }
        // { expiresIn: 1 * 60 } //暂时设置一分钟时长，用来调试
	);
	return token;
};

// 接口返回数据方法封装
function handleResulte (code, data, msg, page, token) {
  if (page) {
      return { code, data, msg, page }
  }
  if (token) {
      return { code, data, msg, token}
  }
  return { code, data, msg }
}

router.post("/user/login",(ctx,next) =>{
    const token = tokenuser(ctx.request.body)
    ctx.body =   handleResulte('202','','成功','',token)
})


router.get("/identity/HZ03", (ctx, next) => {
  var data = Mock.mock({
    // 属性 list 的值是一个数组，其中含有 1 到 10 个元素
    "list|1-10": [
      {
        // 属性 id 是一个自增数，起始值为 1，每次增 1
        "id|+1": 1,
      },
    ],
  });
  let string = JSON.stringify(data.list, null, 4);
  let josn = {
    message: [],
    result: "SUCCESS",
    values: {
      menus: [
        {
          acl: { ability: ["MN2416"], mode: "oneOf" },
          icon: "",
          key: "2416",
          link: "/home",
          target:'',
          title: "仪表盘",
          children: [
            {
              acl: { ability: ["MN2417"], mode: "oneOf" },
              icon: "home",
              key: '2417',
              link: "/v1",
              title: "工作台",
            },
            {
              acl: { ability: ["MN2417"], mode: "oneOf" },
              icon: "home",
              key: '2418',
              link: "/analysis",
              title: "分析页",
            },
            {
              acl: { ability: ["MN2417"], mode: "oneOf" },
              icon: "home",
              key: '2419',
              link: "/monitor",
              title: "监控页",
            },
          ],
        },
        {
          acl: { ability: ["MN0730"], mode: "oneOf" },
          key: 1730,
          title: "表单页",
          link:'',
          children: [
            {
              icon: "basicForm",
              link:'/form/basic-form',
              key: 307,
              title: "基础表单",
            },
            {
              icon: "step-form",
              key: 510,
              link: "/form/step-form",
              title: "分布表单",
            },
            {
              icon: "advanced-form",
              link:'/form/advanced-form', 
              key: 536,
              title: "高级表单",
            },
          ],
        },
        {
          acl: { ability: ["MN0730"], mode: "oneOf" },
          key: 1731,
          title: "列表页",
          link:'',
          children: [
            {
              icon: "basicForm",
              link:'/preview/table-list',
              key: 308,
              title: "查询表格",
            },
            {
              icon: "step-form",
              key: 511,
              link: "/preview/table-list",
              title: "标准表单",
            },
            {
              icon: "advanced-form",
              link:'/preview/table-list', 
              key: 532,
              title: "卡片列表",
            },
          ],
        },
        {
          acl: { ability: ["MN0730"], mode: "oneOf" },
          key: 1736,
          title: "详情页",
          link:'',
          children: [
            {
              icon: "basicForm",
              link:'/details/basic-details',
              key: 2,
              title: "基础详情页",
            },
            {
              icon: "step-form",
              key: 3,
              link: "/details/advanced-detail",
              title: "高级详情页",
            }
          ],
        },
      ],
      roles: ["ROLE_ADMIN", "ROLE_PUBLIC"],
      user: { name: "admin", avatar: "", token: "1" },
    },
  };

  ctx.body = josn;
});


router.post("/analysis/rank",(ctx,next) =>{
  var data =Mock.mock({
    // 属性 list 的值是一个数组，其中含有 1 到 10 个元素
    "list|1-100":[
      {
        key: '1',
        "rank|1-100": 2,
        keyword: Mock.mock('@cname()'),
        "peopleNum|50-100": 20,
        "weekup|2-100": 2,
      }
    ]
  })
  ctx.body = handleResulte('202','','',data)
})

router.post("/table/list",(ctx,next) =>{
  var data =Mock.mock({
    // 属性 list 的值是一个数组，其中含有 1 到 10 个元素
    "list|50-100":[
      {
        "id|+1": 0,
        "ruleName": "TradeCode",
        "describe": "这是一段描述",
        "serviceNum|50-100": 20,
        "state|2-10": 2,
        "lasetime": Mock.mock('@date("yyyy-MM-dd")'),
      }
    ]
  })
  ctx.body = handleResulte('202','','',data)
})

router.post("/form/Members",(ctx,next) =>{
  var data =Mock.mock({
    // 属性 list 的值是一个数组，其中含有 1 到 10 个元素
    "list|1-6":[
      {
        "key|+1": 0,
        name: Mock.mock('@cname()'),
        'workId|1-100':2,
        'belong|1':['工业部','IT部','财务部']
      }
    ]
  })
  ctx.body = handleResulte('202','','',data)
})


module.exports = router.routes();
